/*
 *     This file is part of Mobile GPS Logger.
 * 
 *     Mobile GPS Logger is free software: you can redistribute it and/or
 *     modify it under the terms of the GNU General Public License as
 *     published by the Free Software Foundation, either version 3 of the
 *     License, or (at your option) any later version.
 * 
 *     Mobile GPS Logger is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License v3 for more details.
 * 
 *     You should have received a copy of the GNU General Public License v3
 *     along with Mobile GPS Logger. If not, see <http://www.gnu.org/licenses/>.
*/


package cx.ath.skyflyer.gpslog.util;

/**
 *
 * @author Ahti Legonkov
 */
public class StringUtils {

    /**
     * Works similar to sprintf("%*.*g", width, precision, d);
     * @param d
     * @param width
     * @param precision
     * @return
     */
    public static String toString(double d, int width, int precision) {

        // TODO: make it actually work as described in the comment above.
        long m = 1;
        for (int i = 0; i < precision; ++i) {
            d *= 10.0;
            m *= 10;
        }

        final long l = (long)(d + .5);

        return "" + (l/m) + "." + (l%m);
    }
}
